<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<div class="span12">

	<form id="initForm" action='<c:url value="/system/init/" />'
		class="form-horizontal" method="post" enctype="multipart/form-data">
		<fieldset>
			<c:if test="${message != null }">
				<div class="alert alert-success">
					<c:out value="${message }" />
				</div>
			</c:if>

			<legend>股票信息上传、初始化</legend>
			<c:if test="${!systeminfo.hasUploadInitFile}" var="v1">
				<div class="control-group">
					<label class="control-label" for="oldPassword">股票信息初始化文件<br>(<a
						href='<c:url value="/sample/stocklist.xls" />'>下载sample文件</a>)
					</label>
					<div class="controls">
						<input type="file" id="uploadFile" name="uploadFile">
					</div>
				</div>
				<div class="control-group">
					<div class="controls">
						<button type="submit" class="btn">上传</button>
					</div>
				</div>
			</c:if>

			<c:if
				test="${systeminfo.hasUploadInitFile && !systeminfo.hasInitialized}">
				<div class="control-group">
					<div class="controls">
						<label class="control-label" for="oldPassword">需下载的股票数据年数</label>
					</div>
				</div>
				<div class="control-group">
					<div class="controls">
						<select id="years">
							<option value="1" selected>1年</option>
							<option value="2">2年</option>
							<option value="3">3年</option>
							<option value="4">4年</option>
							<option value="5">5年</option>
						</select>
					</div>
				</div>
				<div class="control-group">
					<div class="controls">
						<button class="btn" id="initbnt">系统初始化</button>
					</div>
				</div>
			</c:if>

			<div id="progressdiv" style="display: none;">
				请不要关闭页面，等待进度条达到100%
				<div class="progress progress-striped active">
					<div class="bar" style="width: 0%;" id="progressbar"></div>
				</div>
			</div>

			<c:if
				test="${systeminfo.hasUploadInitFile && systeminfo.hasInitialized }">
			系统已经完成初始化，想要重新初始化，请联系管理员
		</c:if>

		</fieldset>
	</form>
</div>

<script type="text/javascript">
	$(document).ready(function() {
		//defaults
		$.validator.setDefaults({
			submitHandler : function(form) {
				form.submit();
			}
		});

		//begin to validate
		$('#initForm').validate({
			rules : {
				uploadFile : {
					required : true
				}
			},
			messages : {
				uploadFile : {
					required : "请指定上传文件"
				}
			}
		});

		$("#initbnt").click(function() {
			// 显示进度条
			$("#progressdiv").css("display", "block");
			// 构造iframe
			//$("body").append("<iframe id='comet-frame' style='display: none;'></iframe>");
			//$('#comet-frame').attr("src",'<c:url value="/system/asyn/init" />'); 
			var url = '<c:url value="/system/asyn/doinit?years=" />' + $("#years").val();
			$.get(url, function(data) {
				update(data);
			});

			return false;

		});
	});

	var stop = false;

	function update(percentage) {
		$("#progressbar").css("width", percentage);
		$.get('<c:url value="/system/asyn/initprogress" />', function(data) {
			if (!stop) {
				update(data);
				if (data == '100%') {
					stop = true;
					alert('初始化完毕');
					$("#initbnt").attr("display", "none");
				}
			}
		});
	}
</script>


